<?php
namespace wogu2\http;

use ArrayObject;
use wogu2\stdlib\ResponseInterface;
use wogu2\http\header\HeaderInterface;
use wogu2\http\header\MultipleHeaderInterface;

class Response implements ResponseInterface
{
    private $_runtime = null;
    
    public function setRuntime(ArrayObject $runtime) {
        $this->_runtime = $runtime;
    }
    
    public function send() {
        $this->_sendHeaders();
        echo end($this->_runtime->body);
    }
    
    private function _sendHeaders() {
        foreach($this->_runtime->headers as $header) {
            if($header instanceof MultipleHeaderInterface) {
                header($header->toString(), false);
            } elseif($header instanceof HeaderInterface) {
                header($header->toString());
            } else {
                header($header);
            }
        }
    }
}